package UILag;
import java.util.Scanner;
import ctrLag.*;
import modelLag.*;

/**
 * Write a description of class FriendsUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDsUI {
    // instance variables - replace the example below with your own
    private DvdCtr dCtr;

    /**
     * Constructor for objects of class FriendsUI
     */
    public DVDsUI() {
        dCtr = new DvdCtr();
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public void start() {
        dvdsMenu();
    }

    public void dvdsMenu() {
        boolean exit = false;
        while(!exit) {
            // that is: while exit is false
            int choise = writeDVDsMenu(); 
            if(choise == 1) {
                addDVD();
            }
            else if(choise == 2) {
                findDVD();
            }
            else if(choise == 3) {
                updateDVD();
            }
            else if (choise == 4) {
                deleteDVD();
            }
            else if (choise == 5) {
                addCopy();
            }
            else if (choise ==6) {
                findCopy();
            }

            else if (choise == 7) {
                deleteCopy();              
            }          
            else {
                exit = true;
            }
        }//end else    
    }//end while

    public int writeDVDsMenu() {
        // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** DVDs Menu ***");
        System.out.println(" (1) Add DVD");
        System.out.println(" (2) Find DVD");
        System.out.println(" (3) Edit DVD");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Add Copy");
        System.out.println(" (6) Find Copy");
        System.out.println(" (7) Delete Copy");
        System.out.println(" (8) Close");
        System.out.print("\n Make your choice: ");

        int choise = keyboard.nextInt();
        if(choise > 8 || choise == 0) {
            System.out.println("WTF.. You choose " + choise +" this is not a valid number.......");
            return writeDVDsMenu();
        }
        else
            return choise;
    }

    public String inputTitle() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the title: ");
        String title = keyboard.nextLine();
        return title;
    }

    public String inputArtist() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the artist: ");
        String artist = keyboard.nextLine();
        return artist;
    }

    public String inputPublicationDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the publication date: ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }

    public int inputBarcode() {
        boolean ok = false;
        int barcode = 0;
        while(!ok) {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Enter the barcode: ");
            try {
                barcode = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e) {
                System.out.println("Input must be a number, please try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return barcode;
    }

    public int inputSerialNumber() {
        boolean ok = false;
        int serialNumber = 0;
        while(!ok) {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Enter the serial number of the copy: ");
            try {
                serialNumber = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e) {
                System.out.println("Input must be a number, please try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return serialNumber;
    }

    public String inputPurchaseDate() {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the purchase date of the copy: ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }

    public double inputPurchasePrice() {
        boolean ok = false;
        double purchasePrice = 0;
        while(!ok) {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Enter the purchase price of the copy: ");
            try {
                purchasePrice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e) {
                System.out.println("Input must be a number, please try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return purchasePrice;
    }

    public void addDVD() {
        int barcode = inputBarcode();
        String title = inputTitle();
        String artist = inputArtist();
        String publicationDate = inputPublicationDate();

        dCtr.addDVD(barcode, title, artist, publicationDate);
    }

    private DVD findDVD() {
        int barcode = inputBarcode();
        DVD dvd = dCtr.findDVD(barcode);
        try {
            dvd.printDVDInfo();
            return dvd;
        }
        catch(Exception e) {
            System.err.println("We didn't find any DVD with that barcode.");
        }        
        return dvd;
    }

    public void deleteDVD() {      
        String title = inputTitle();
        dCtr.deleteDVD(title);
    }

    public void updateDVD() {
        printDvdList(); 
        int barcode = inputBarcode();
        DVD dvd = dCtr.findDVD(barcode);
        if (dvd != null) {
            dvd.printDVDInfo();
            System.out.println("Update this DVD");
            int newBarcode = inputBarcode();
            String newTitle = inputTitle();
            String newArtist = inputArtist();
            String newPublicationDate = inputPublicationDate();
            dCtr.updateDVD(dvd, newBarcode, newTitle, newArtist, newPublicationDate);
        }
        else {
            System.out.println("Error! DVD is not registered in system.");
        }
    }

    public void addCopy() {
        printDvdList(); 
        System.out.println("Find the DVD you want to add a copy of");
        int d = inputBarcode();
        DVD dvd = dCtr.findDVD(d);
        if (dvd == null) {
            System.out.println("Error! DVD not in system");
        }
        else {
            System.out.println("\n");
            int serialNumber = inputSerialNumber();
            String purchaseDate = inputPurchaseDate();
            double purchasePrice = inputPurchasePrice();      
            dCtr.addCopy(dvd, serialNumber, purchaseDate, purchasePrice);
        }
    }

    private Copy findCopy() {
        printDvdList();
        System.out.println("Select DVD");
        int barcode = inputBarcode(); 
        System.out.println("Now the specific Copy");
        int serialNumber = inputSerialNumber();
        DVD dvd = dCtr.findDVD(barcode);
        Copy copy = dCtr.findCopy(dvd, serialNumber);
        return copy;

    }

    public void deleteCopy() {
        printDvdList();
        System.out.println("First select DVD");
        int barcode = inputBarcode();
        System.out.println("Now find the copy you want to delete");
        int serialNumber = inputSerialNumber();
        dCtr.deleteCopy(barcode, serialNumber);
    }

    public void printDvdList() {
        dCtr.printDVDList();
    }
}










